package com.mcxx.modules.miniSalvation.family.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.family.dao.write.FamilySupportWriteDAO;
import com.mcxx.modules.miniSalvation.family.entity.FamilySupportEntity;
import com.mcxx.modules.miniSalvation.family.service.read.FamilySupportReadService;
import com.mcxx.modules.miniSalvation.order.service.write.OrderCertWriteService;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Slf4j
@Service("caregiverWriteService")
@TaTransactional
public class FamilySupportWriteService {
    @Autowired
    private FamilySupportWriteDAO familySupportWriteDao;
    @Autowired
    private FamilySupportReadService familySupportReadService;
    @Autowired
    private OrderCertWriteService orderCertWriteService;


    public void addFamilySupport(FamilySupportEntity familySupportEntity) {

        familySupportReadService.verifyIdcardInFamily(familySupportEntity.getFamilyId(),familySupportEntity.getIdcard(), familySupportEntity.getId());

        familySupportReadService.verifyIdcardInFamilyCount(familySupportEntity.getFamilyId(),familySupportEntity.getIdcard(), familySupportEntity.getSupportName());


        familySupportEntity.setId(Constant.getUUID());

        int rows = familySupportWriteDao.insertFamilySupportFatch(familySupportEntity);
        if (rows == 0) {
            log.error(FailureCode.ERR_9999.getMsg());
            throw new BizException(FailureCode.ERR_9999);
        }
    }


    public void addRecheckFamilySupport(FamilySupportEntity familySupportEntity,String orderId,String memberId) {

//        familySupportReadService.verifyIdcardInFamily(familySupportEntity.getFamilyId(),familySupportEntity.getIdcard(), familySupportEntity.getId());


        String supportId = Constant.getUUID();
        familySupportEntity.setId(supportId);


        orderCertWriteService.copyMarriedCertInfo(orderId,supportId,memberId);


        int rows = familySupportWriteDao.insertFamilySupportFatch(familySupportEntity);
        if (rows == 0) {
            log.error(FailureCode.ERR_9999.getMsg());
            throw new BizException(FailureCode.ERR_9999);
        }
    }


    public void delFamilySupportById(String id) {
        int rows = familySupportWriteDao.deleteFamilySupportFatch(id);
        if (rows == 0) {
            log.error(FailureCode.ERR_10003.getMsg());
            throw new BizException(FailureCode.ERR_10003);
        }
    }


    public void delFamilySupportByIdcard(String idcard,String familyId) {
        familySupportWriteDao.delFamilySupportByIdcard(idcard,familyId);

    }


    public void modifyFamilySupport(FamilySupportEntity entity) {
//        familySupportReadService.verifyIdcardInFamilyCount(entity.getFamilyId(),entity.getIdcard(), entity.getSupportName());


        int rows =familySupportWriteDao.updateFamilySupportById(entity);

        if (rows == 0) {
            log.error(FailureCode.ERR_10003.getMsg());
            throw new BizException(FailureCode.ERR_10003);
        }
    }


    public void copyFamilySupportInfo(String familyId) {
        familySupportWriteDao.copyFamilySupportInfo(familyId);
    }

    public void deleteFamilySupportById(String familyId) {
        familySupportWriteDao.deleteFamilySupportById(familyId);

    }

}
